package com.wang.decagency.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wang.decagency.entity.GDeclarationDocument;
import com.wang.decagency.entity.SysBase;
import com.wang.decagency.entity.SysUser;
import com.wang.decagency.service.impl.SysBaseServiceImpl;
import com.wang.utils.ResultEntity;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wang
 * @since 2023-05-29
 */
@Slf4j
@RestController
@Api(tags = "系统参数")
@RequestMapping("/decagency/base")
public class SysBaseController {

    @Autowired
    SysBaseServiceImpl sysBaseService;

    @PostMapping("/addSysBase")
    @ApiOperation(value = "添加码表", notes = "添加码表")
    public Object addSysBase(@RequestBody @ApiParam(name = "SysUser", value = "码表信息", required = true) SysBase sysBase) {
        if (Objects.isNull(sysBase) || sysBase.getType() == null || sysBase.getCode() == null || sysBase.getName() == null) {
            return ResultEntity.fail("入参不全");
        }
        sysBaseService.saveOrUpdate(sysBase);
        return ResultEntity.ok();
    }


    @PostMapping("/updateSysBase")
    @ApiOperation(value = "修改码表", notes = "修改码表")
    public Object updateSysBase(@RequestBody @ApiParam(name = "SysUser", value = "码表信息", required = true) SysBase sysBase) {
        if (Objects.isNull(sysBase) || sysBase.getId() == null) {
            return ResultEntity.fail("入参不全");
        }
        sysBaseService.updateById(sysBase);
        return ResultEntity.ok();
    }


    @PostMapping({"/findSysBase","/findSysBaseByType"})
    @ApiOperation(value = "查询码表", notes = "查询码表")
    public Object findSysBase(@RequestBody Map<String, Object> mapParam, HttpServletRequest request) {
        int pageNum = mapParam.get("pageNum") != null ? (int) mapParam.get("pageNum") : 1;
        int pageSize = mapParam.get("pageSize") != null ? (int) mapParam.get("pageSize") : 5;
        SysBase sysBase = JSON.parseObject(JSON.toJSONString(mapParam), SysBase.class);
        log.info("path:"+request.getRequestURI());

        if ( request.getRequestURI().contains("findSysBaseByType") && ( Objects.isNull(sysBase) || sysBase.getType() == null)) {
            return ResultEntity.fail("入参不全,缺少type");
        }
        QueryWrapper queryWrapper = new QueryWrapper<>(sysBase);
        Page<SysBase> page = new Page<>(pageNum, pageSize);
        IPage<SysBase> SysUserList = sysBaseService.page(page, queryWrapper);
        return ResultEntity.ok(SysUserList);
    }

    @PostMapping("/getByKey")
    @ApiOperation(value = "查询详情", notes = "查询详情")
    @ApiResponses({@ApiResponse(code = 2000, message = "成功")})
    public Object getByKey(@RequestBody @ApiParam(name = "cost", value = "报关单信息") SysBase obj) throws Exception {
        obj = sysBaseService.getById(obj.getId());
        return ResultEntity.ok(obj);
    }

    @PostMapping("/delByKeys")
    @ApiOperation(value = "根据主键删除", notes = "根据主键删除")
    @ApiResponses({@ApiResponse(code = 2000, message = "成功")})
    public Object delByKeys(@RequestBody List<Integer> ids) throws Exception {
        boolean b = sysBaseService.removeByIds(ids);
        return ResultEntity.ok(b);
    }

}
